package com.koron.operation.classes.info.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.OrderItem;
import com.koron.bean.query.PageQuery;
import com.koron.operation.classes.info.bean.dto.ClassesClassifyDTO;
import com.koron.operation.classes.info.bean.po.ClassesClassify;
import com.koron.operation.classes.info.bean.po.ClassesClassifyItem;
import com.koron.operation.classes.info.bean.query.ClassesQuery;
import com.koron.operation.classes.info.mapper.ClassesClassifyMapper;
import com.koron.operation.classes.info.service.ClassesClassifyItemService;
import com.koron.operation.classes.info.service.ClassesClassifyService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 班别分类表(ClassesClassify)表服务实现类
 *
 * @author jzp
 * @since 2023-05-25 10:11:06
 */
@Service
public class ClassesClassifyServiceImpl implements ClassesClassifyService {

    @Autowired
    private ClassesClassifyItemService classesClassifyItemService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ClassesClassify getById(SessionFactory factory, String id) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public ClassesClassifyDTO getDtoById(SessionFactory factory, String id) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        return mapper.selectDtoById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ClassesClassify> list(SessionFactory factory, ClassesQuery query) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<ClassesClassify> page(SessionFactory factory, PageQuery pageQuery, ClassesQuery query) {
        pageQuery.getOrders().add(new OrderItem("sort", true));
        pageQuery.getOrders().forEach(e -> e.setPrefix("classesClassify"));
        return ClassesClassifyService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ClassesClassify classesClassify) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        if (classesClassify.getSort() == null) {
            Integer maxSort = mapper.getMaxSort();
            classesClassify.setSort(maxSort == null ? 0 : maxSort + 1);
        }
        return mapper.insertOrUpdate(classesClassify) > 0;
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdateDto")
    public boolean saveOrUpdateDto(SessionFactory factory, ClassesClassifyDTO classesClassifyDTO) {
        // 1. 保存或更新基本数据
        if (StrUtil.isEmpty(classesClassifyDTO.getId())) {
            // 1.1. 保存基本数据
            save(factory, classesClassifyDTO);
        } else {
            // 1.1. 更新数据，原数据逻辑删除，然后插入一条数据
            deleteById(factory, classesClassifyDTO.getId());
            // 1.2. 清空id
            classesClassifyDTO.setId(null);
            // 1.3. 保存基本数据
            save(factory, classesClassifyDTO);
        }

        // 2. 保存班别列表数据
        List<ClassesClassifyItem> items = classesClassifyDTO.getItems();
        if (items == null) {
            return true;
        }
        checkDuplicated(factory, items);
        // 2.1. 设置主表id，并清空id
        items.forEach(e -> {
            e.setClassesId(classesClassifyDTO.getId());
            e.setId(null);
        });
        // 2.2. 批量插入
        return classesClassifyItemService.saveBatch(factory, items);
    }

    private boolean checkDuplicated(SessionFactory factory, List<ClassesClassifyItem> itemList) {
        long count = itemList.stream().map(ClassesClassifyItem::getName).distinct().count();
        Assert.state(itemList.size() == count, "班别名称不能重复");
        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ClassesClassify classesClassify) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        return mapper.update(classesClassify) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        ClassesClassifyMapper mapper = factory.getMapper(ClassesClassifyMapper.class);
        return mapper.hidden(id) > 0;
    }
}
